package org.arain.spring.captive.portal.server;

import java.net.InetSocketAddress;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
/**
 * 
* <p>Title:UDPServer </p>
* @author Arain
* @date2018年8月10日
 */
@Component
public class UDPServer {
	@Autowired
    @Qualifier("bootstrap")
    private Bootstrap b;

    @Autowired
    @Qualifier("udpSocketAddress")
    private InetSocketAddress udpPort;

    private ChannelFuture serverChannelFuture;

    @PostConstruct
    public void start() throws Exception {
        System.out.println("====Starting UDP server at==== " + udpPort);
        serverChannelFuture = b.bind(udpPort).sync();
    }

    @PreDestroy
    public void stop() throws Exception {
        serverChannelFuture.channel().closeFuture().sync();
    }

    public Bootstrap getB() {
        return b;
    }

    public void setB(Bootstrap b) {
        this.b = b;
    }

    public InetSocketAddress getUdpPort() {
        return udpPort;
    }

    public void setUdpPort(InetSocketAddress udpPort) {
        this.udpPort = udpPort;
    }
}
